from django.shortcuts import render,HttpResponse,redirect,reverse
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login,logout,authenticate
from .models import Users,Teachar,Student,Type,Vacate
from django.contrib import auth
from django.core.paginator import Paginator
from django.views import View
# Create your views here.
def index(request):
    return HttpResponse('ok')

# 注册
@login_required
def register(request):
    if request.method == 'GET':
        return render(request,'register.html')
    if request.method == 'POST':
        uname = request.POST.get('uname')
        upwd = request.POST.get('upwd')
        role = request.POST.get('role')
        if role == '1':
            Student.objects.create(password=upwd,name=uname)
            return HttpResponse('学生注册成功')
        if role == '2':
            Teachar.objects.create(name=uname,password=upwd)
            return HttpResponse('教师注册成功')
    else:
        return render(request,'register.html',{'a':'注册失败！'})


class loginx(View):
    def get(self,reqeust):
        return render(reqeust,'loginx.html')
    def post(self,request):
        uname = request.POST.get('uname')
        upwd = request.POST.get('upwd')
        role = request.POST.get('role')
        print(role)
        if role == '1':
            if Student.objects.filter(name=uname,password=upwd):
                return redirect('app1:add_Vacate')
            else:
                return HttpResponse('密码或者账户错误')
        if role == '2':
           if Teachar.objects.filter(name=uname,password=upwd):
               return redirect('app1:show_Vacate')
           else:
               return HttpResponse('密码或者账户错误')
        else:
            return HttpResponse('请选择角色')

@login_required
def add_type(request):
    if request.method == 'GET':
        return render(request,'add_type.html')
    if request.method == 'POST':
        tname = request.POST.get('tname')
        Type.objects.create(tname=tname)
        return HttpResponse('添加成功')


def add_Vacate(request):
    if request.method == 'GET':
        listl = Type.objects.all()
        return render(request,'add_Vacate.html',{'listl':listl})
    if request.method == 'POST':
        stu_name = request.POST.get('stu_name')
        createtime = request.POST.get('createtime')
        context = request.POST.get('context')
        eid = request.POST.get('eid')
        Vacate.objects.create(stu_name=stu_name,createtime=createtime,context=context,tid_id=eid)
        return HttpResponse('添加成功')


def show_Vacate(request):
    c_show = Vacate.objects.all()
    listl = Type.objects.all()
    p = Paginator(c_show, 3)
    max_page = p.num_pages
    x = request.GET.get('page', 1)
    try:
        x = int(x)
        if x < 1:
            x = 1
        if x > max_page:
            x = max_page
    except Exception as e:
        x = 1
    pagex = p.get_page(x)
    if request.GET:
        tname = request.GET.get('tname')
        if tname:
            pagex = Type.objects.filter(tname=tname)
    return render(request, 'show_Vacate.html', {'pagex': pagex, 'current_page': x, 'max_page': max_page,'listl':listl})

@login_required(login_url='/leaves/loginx')
def needlogin(request):
    return HttpResponse('这是一个需要登录才能访问的页面')

# 注销
def logoutt(request):
    logout(request)
    return redirect('app1:index')

# 通过不通过
def passs(request,id):
    status=request.POST.get('status')
    Vacate.objects.filter(id=id).update(status='通过')
    return HttpResponse('通过')

def pas(request,id):
    status=request.POST.get('status')
    Vacate.objects.filter(id=id).update(status='不通过')
    return HttpResponse('不通过')

